package com.dreamstall.xiaome;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Spider {

	public String getContext(String url, String type)
			throws ClientProtocolException, IOException {
		String context = "";
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		// Execute the request
		HttpResponse response = httpclient.execute(httpget);
		StringBuffer sb = new StringBuffer();

		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instream = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					instream, type));
			while (true) {
				String line = br.readLine();
				if(line==null)break;
				sb.append(line);
			}
			context = sb.toString();
			instream.close();
			httpget.abort();
			httpclient.getConnectionManager().shutdown();
		}
		return context;
	}

	public Set<String> getWords() {
		String context = "";
		Set<String> words = new HashSet<String>();
		try {
			context = getContext("http://www.engadget.com/", "utf-8");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String regixForBrand = " ([a-zA-Z]*) ";
		Pattern patternForBrand = Pattern.compile(regixForBrand);
		Matcher maForBrand = patternForBrand.matcher(context);
		while (maForBrand.find()) {
			String word = maForBrand.group(1);
			word = word.trim();
			if (!word.equals("")) {
				word = word.toLowerCase();
				words.add(word);
			}
		}
		System.out.println(words.size());
		return words;
	}

	public Word translate(String word) {
		Word value = new Word();
		value.setWord(word);
		String context = "";

		try {
			for (int i = 0; i < 50; i++) {
				context = getContext("http://www.iciba.com/" + word, "utf-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String regixProns = "<div class=\"prons\">(.*?)</div>";
		Pattern patternProns = Pattern.compile(regixProns);
		Matcher maProns = patternProns.matcher(context);
		while (maProns.find()) {
			String prons = maProns.group(1);
			if (!prons.equals("")) {
				String regixForEn = "\\[(.)\\]";
				Pattern patternForEn = Pattern.compile(regixForEn);
				Matcher maForEn = patternForEn.matcher(prons);

				String regixForProns = "US\">(.*?)</strong>";
				Pattern patternForProns = Pattern.compile(regixForProns);
				Matcher maForProns = patternForProns.matcher(prons);
				boolean done = false ;
				while (!done) {
					done = true ;
					String pronunciation = "" ;
					if(maForEn.find()){ 
						pronunciation +=  maForEn.group(1);
						done=false;
					}
					if(maForProns.find()){
						pronunciation += "[" + maForProns.group(1) + "]";
						done=false;
					}
					value.setPronunciation(value.getPronunciation()+pronunciation);
				}
			}
		}
		
		String regixForBrand = "<!--小白词典 Start-->(.*?)<!--小白词典 End-->";
		Pattern patternForBrand = Pattern.compile(regixForBrand);
		Matcher maForBrand = patternForBrand.matcher(context);
		while (maForBrand.find()) {
			String prons = maForBrand.group(1);
			if (!prons.equals("")) {
				String regixForEn = "<strong class=\"fl\">(.*?)</strong>";
				Pattern patternForEn = Pattern.compile(regixForEn);
				Matcher maForEn = patternForEn.matcher(prons);

				String regixForProns = "<span class=\"label_list\">(.*?)</span>";
				Pattern patternForProns = Pattern.compile(regixForProns);
				Matcher maForProns = patternForProns.matcher(prons);
				boolean done = false ;
				while (!done) {
					done = true ;
					String mean = "" ;
					if(maForEn.find()){ 
						mean +=  maForEn.group(1);
						done=false;
					}
					if(maForProns.find()){
						String means = maForProns.group(1);
						means = means.trim();
						means = means.replaceAll("	", "");
						means = means.replaceAll("<label>", "");
						means = means.replaceAll("</label>", "");
						mean += means;
						done=false;
					}
					value.getMeans().add(mean);
				}
			}
		}
		return value;
	}

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		Set<String> words = new Spider().getWords();
		Object[] words_array = words.toArray();
		String msg = "";
		while (true) {
			String word = (String) words_array[new Random()
					.nextInt(words_array.length)];
//			msg = new Spider().translate(word);
			if (!msg.equals(word))
				break;
		}
		new Spider().getContext("http://dreamstall.com/msn/?msg="
				+ URLEncoder.encode(msg), "utf-8");

	}
}
